﻿// -------------------------------------------------------------------------
//    @FileName         :    NFWorldLoginModule.cpp
//    @Author           :    gaoyi
//    @Date             :   2023-07-10
//    @Email			:    445267987@qq.com
//    @Module           :    NFWorldLoginModule
//
// -------------------------------------------------------------------------

#include "NFLogicPlayerModule.h"

#include <NFLogicConfig.h>
#include <NFLoginNoticeData.h>
#include <Base/NFRegisterCache.h>
#include <Base/NFServerUtil.h>
#include <GlobalData/NFGlobalMiscData.h>
#include <NFServerComm/NFServerCommon/NFDBObjMgr.h>

#include "NFServerLevelMgr.h"
#include "NFWelfareMgr.h"
#include "Player/NFPlayer.h"
#include "Player/NFPlayerMgr.h"
#include "NFServerComm/NFServerCommon/NFILogicServerModule.h"


NFCLogicPlayerModule::NFCLogicPlayerModule(NFIPluginManager *p) : NFMMODynamicModule(p)
{
}

NFCLogicPlayerModule::~NFCLogicPlayerModule()
{
}

int NFCLogicPlayerModule::Awake()
{
    FindModule<NFILogicServerModule>()->SetCheckStoreServer(true);
    FindModule<NFILogicServerModule>()->SetCheckWorldServer(true);
    NFServerConfig *pConfig = FindModule<NFIConfigModule>()->GetAppConfig(NF_ST_LOGIC_SERVER);
    NF_ASSERT(pConfig);
    int iRetCode = pConfig->GetExternData(NFLogicConfig::Instance()->m_config);
    CHECK_RET(iRetCode, "GetExternData Config Data, failed");
    NFLogInfo(NF_LOG_DEFAULT, 0, "logic config:{}", NFLogicConfig::Instance()->m_config.ShortDebugString())

    //FindModule<NFILogicServerModule>()->SetCheckCenterServer(true);
    ////////////proxy msg////player login,disconnect,reconnet/////////////////////

    RegisterAppTask(NF_ST_LOGIC_SERVER, APP_INIT_DESC_STORE_LOAD, GetServerName(NF_ST_LOGIC_SERVER) + " Load Desc Store", APP_INIT_TASK_GROUP_SERVER_LOAD_DESC_STORE);
    if (EN_OBJ_MODE_INIT == NFShmMgr::Instance()->GetRunMode())
    {
        RegisterAppTask(NF_ST_LOGIC_SERVER, APP_INIT_LOAD_GLOBAL_DATA_DB,
                        NF_FORMAT("{} {}", pConfig->ServerName, "Load Logic Global Data From Store Server"), APP_INIT_TASK_GROUP_SERVER_LOAD_OBJ_FROM_DB);
        RegisterAppTask(NF_ST_LOGIC_SERVER, APP_INIT_SYNC_SERVER_GLOBAL_DATA,
                NF_FORMAT("{} {}", pConfig->ServerName, "Sync Logic Global Data From World Server"), APP_INIT_TASK_GROUP_SERVER_SYNC_GLOBAL_DATA);
    }

    NFServerLevelMgr::Instance()->Init();

    return 0;
}

int NFCLogicPlayerModule::Init()
{
    uint64_t serverLinkId = FindModule<NFIMessageModule>()->GetServerLinkId(NF_ST_LOGIC_SERVER);
    CHECK_EXPR_ASSERT(serverLinkId > 0, -1, "logic server link id <= 0");

    FindModule<NFIMessageModule>()->AddOtherCallBack(NF_ST_LOGIC_SERVER, serverLinkId, this, &NFCLogicPlayerModule::OnHandleServerOtherMessage);
    return 0;
}

int NFCLogicPlayerModule::Tick()
{
    if (m_pObjPluginManager->IsInited(NF_ST_LOGIC_SERVER))
    {
        NFServerUtil::Tick();
    }
    return 0;
}

int NFCLogicPlayerModule::OnDynamicPlugin()
{
    return 0;
}

int NFCLogicPlayerModule::OnHandleClientMessage(uint32_t msgId, NFDataPackage &packet, uint64_t param1, uint64_t param2)
{
    if (!m_pObjPluginManager->IsInited(NF_ST_LOGIC_SERVER))
    {
        NFLogError(NF_LOG_DEFAULT, packet.nParam1, "Logic Server not inited, drop client msg:{}", packet.ToString());
        return -1;
    }

    if (m_pObjPluginManager->IsServerStopping())
    {
        NFLogError(NF_LOG_DEFAULT, packet.nParam1, "Logic Server is Stopping, drop client msg:{}", packet.ToString());
        return -1;
    }

    /*switch (packet.nMsgId)
    {
        default:
        {
            NFLogError(NF_LOG_DEFAULT, 0, "Client MsgId:{} Register, But Not Handle, Package:{}", packet.nMsgId, packet.ToString());
            break;
        }
    }*/
    return 0;
}


int NFCLogicPlayerModule::OnHandleServerMessage(uint32_t msgId, NFDataPackage &packet, uint64_t param1, uint64_t param2)
{
    if (!m_pObjPluginManager->IsInited(NF_ST_LOGIC_SERVER))
    {
        NFLogError(NF_LOG_DEFAULT, packet.nParam1, "Logic Server not inited, drop client msg:{}", packet.ToString());
        return -1;
    }

    if (m_pObjPluginManager->IsServerStopping())
    {
        NFLogError(NF_LOG_DEFAULT, packet.nParam1, "Logic Server is Stopping, drop client msg:{}", packet.ToString());
        return -1;
    }

    switch (packet.nMsgId)
    {
        default:
        {
            NFLogError(NF_LOG_DEFAULT, 0, "Server MsgId:{} Register, But Not Handle, Package:{}", packet.nMsgId, packet.ToString());
            break;
        }
    }
    return 0;
}

int NFCLogicPlayerModule::AfterAllConnectAndAllDescStore(NF_SERVER_TYPE serverType)
{
    return 0;
}

int NFCLogicPlayerModule::AfterObjFromDBLoaded(NF_SERVER_TYPE serverType)
{
    return 0;
}

int NFCLogicPlayerModule::OnHandleServerOtherMessage(uint64_t unLinkId, NFDataPackage& packet)
{
    LOG_ERR(0, -1, "msgId:{} msg:{} not handled!", proto_ff::Proto_CS_CMD_Name(static_cast<proto_ff::Proto_CS_CMD>(packet.nMsgId)), packet.ToString());
    return -1;
}
